<template>
    <view class="bg-content">
        <div style="padding: 15px; background-color: white;">
            <u-form ref="validateFormRef" :model="validateForm.value" :label-width="150">

                <u-form-item label="配送类型" prop="deliveryType" required>
                    <u-input type="select" :select-open="validateForm.controls.deliveryType.isShow" v-model="validateForm.controls.deliveryType.textDesc"
                             placeholder="请选择配送类型" @click="validateForm.controls.deliveryType.isShow = true; $forceUpdate();"></u-input>
                </u-form-item>

                <div v-if="validateForm.value.deliveryType === '1'">
                    <u-form-item label="收件人" prop="consignee" required>
                        <u-input v-model="validateForm.value.consignee" placeholder="请输入寄件人"/>
                    </u-form-item>
                    <u-form-item label="手机号码" prop="phone" required>
                        <u-input v-model="validateForm.value.phone" placeholder="请输入收件人手机号码"/>
                    </u-form-item>
                    <u-form-item label="所在地区" prop="region" required>
                        <u-input type="select" :select-open="validateForm.controls.region.isShow"
                                 v-model="validateForm.value.region" placeholder="省市区县、乡镇等"
                                 @click="validateForm.controls.region.isShow = true; $forceUpdate();"/>
                    </u-form-item>
                    <u-form-item label="详细地址" prop="address" required>
                        <u-input type="textarea" height="40" placeholder="街道、楼牌等" v-model="validateForm.value.address"/>
                    </u-form-item>

                    <u-form-item label="快递公司" prop="logisticsCompanyName" required>
                        <u-input v-model="validateForm.value.logisticsCompanyName" type="select" placeholder="请选择快递公司" right-icon="arrow-right"
                                 @click="navigateTo(`/pages/common/logistics-company-select/index`, null, validateForm.value)"/>
                    </u-form-item>
                    <u-form-item label="快递单号" prop="logisticsNo" required>
                        <u-input v-model="validateForm.value.logisticsNo" placeholder="请输入快递单号"/>
                    </u-form-item>
                    <u-form-item label="选择寄件人" prop="storeSenderId" required right-icon="arrow-right">
                        <u-input v-model="validateForm.value.storeSenderText" type="textarea" height="40" :clearable="false" :disabled="true" placeholder="请选择选择寄件人" right-icon="arrow-right"
                                 @click="navigateTo(`/pages/sub/sub2-store-platform/store-platform/store-platform-store-manage/store-platform-store-sender/store-platform-store-sender-select/index`, null, validateForm.value)"/>
                    </u-form-item>
                </div>
            </u-form>
            <u-button type="error" @click="submit()">提交</u-button>

            <u-select mode="single-column" label-name="name" value-name="code" :mask-close-able="false"
                      v-model="validateForm.controls.deliveryType.isShow" :list="validateForm.controls.deliveryType.options"
                      :default-value="validateForm.controls.deliveryType.defaultValue"
                      @cancel="validateForm.controls.deliveryType.isShow = false; $forceUpdate();"
                      @confirm="UFormUtil.selectSingleColumnConfirm($event, validateForm.controls.deliveryType)"></u-select>

            <u-picker mode="region" :mask-close-able="false" v-model="validateForm.controls.region.isShow" v-if="validateForm.value.provinceName"
                      :default-region="[validateForm.value.provinceName, validateForm.value.cityName, validateForm.value.areaName]"
                      @cancel="validateForm.controls.region.isShow = false; $forceUpdate();"
                      @confirm="UFormUtil.regionConfirm($event, validateForm.controls.region)"></u-picker>
        </div>
    </view>
</template>

<script>

    import {CommonUtil} from "@/dev/core/utils/common.util";
    import {TbOrderService} from "@/dev/services/tb-order.service";
    import {UFormUtil} from "@/dev/core/utils/u-form.util";
    import {SysDictionaryService} from "@/dev/services/sys-dictionary.service";

    export default {
        data() {
            return {
                pickerShow: false,
                tbOrder: null,
                validateForm: {
                    value: {
                        id: '',
                        status: '2',  // 改成已发货
                        deliveryType: '1',  //   `delivery_type` varchar(100) DEFAULT NULL COMMENT '配送类型：0-线下速派，1-快递公司',
                        logisticsCompanyType: '',
                        logisticsCompanyName: '',
                        logisticsNo: '',

                        // 收件人
                        consignee: '',
                        phone: '',
                        region: '',  // ['北京', '北京市', '海淀区']
                        provinceName: '',
                        cityName: '',
                        areaName: '',
                        address: '',

                        // 寄件人
                        storeSenderId: '',  // 寄件人信息
                        storeSenderText: '',  // 寄件人信息
                    },
                    rules: {
                        deliveryType: [
                            {required: true, message: '必填项!', trigger: ['change', 'blur']},
                            {pattern: /^(?=.*\S).+$/, message: '必填项!', trigger: ['change', 'blur']},
                        ],
                        consignee: [
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (this.validateForm.value.deliveryType === '1' && !value) {
                                        callback(new Error('请输入收件人名称！'))
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ],

                        phone: [
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (this.validateForm.value.deliveryType === '1' && !value) {
                                        callback(new Error('请输入收件人手机号码！'))
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ],

                        region: [
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (this.validateForm.value.deliveryType === '1' && !value) {
                                        callback(new Error('请选择收件人省市区县、乡镇等！'))
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ],

                        address: [
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (this.validateForm.value.deliveryType === '1' && !value) {
                                        callback(new Error('请输入收件人街道、楼牌等！'))
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ],
                        logisticsCompanyName: [
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (this.validateForm.value.deliveryType === '1' && !value) { // 快递公司
                                        callback(new Error('请选择快递公司！'))
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ],
                        logisticsNo: [
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (this.validateForm.value.deliveryType === '1' && !value) { // 快递公司
                                        callback(new Error('请输入快递单号！'))
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ],
                        storeSenderId: [
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    if (this.validateForm.value.deliveryType === '1' && !value) { // 快递公司
                                        callback(new Error('请选择选择寄件人！'))
                                    } else {
                                        callback()
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        },
        onReady() {
            const _self = this;
            _self.tbOrder = _self.objParams;
            UFormUtil.setDefaultValues(this.validateForm, _self.tbOrder);
            _self.validateForm.value.status = "2";
            _self.validateForm.value.deliveryType = "1";
            _self.validateForm.value.region = _self.objParams.provinceName + '-' + this.objParams.cityName + '-' + this.objParams.areaName;
            new SysDictionaryService().optionList('tb_order@delivery_type', null).then(result => {
                UFormUtil.setOptions(_self.validateForm.controls.deliveryType, result);
                _self.$forceUpdate();
            });

            console.log(_self.validateForm.controls.deliveryType)
        },
        methods: {
            // selectLogisticsCompanyName($event) {
            //     console.log($event);
            //     const _self = this;
            //     _self.validateForm.controls.deliveryType.textDesc = $event[0].name;
            //     _self.validateForm.value.deliveryType = $event[0].code;
            //     this.validateForm.controls.deliveryType.isShow = false;
            //     this.$forceUpdate();
            // },
            // 选择地区回调
            // regionConfirm(e) {
            //     this.validateForm.value.region = e.province.label + '-' + e.city.label + '-' + e.area.label;
            //     this.validateForm.value.provinceName = e.province.label;
            //     this.validateForm.value.cityName = e.city.label;
            //     this.validateForm.value.areaName = e.area.label;
            // },
            submit() {
                const _self = this;
                _self.$refs.validateFormRef.validate(valid => {
                    if (valid) {
                        console.log('验证通过', _self.validateForm.value);
                        CommonUtil.alert({
                            title: '提示！',
                            content: '确定发货吗？',
                            showCancel: true, // 是否显示取消按钮
                            success: function (res) {
                                if (res.cancel) { // 点击取消,默认隐藏弹框
                                } else { // 点击确定
                                    new TbOrderService().updateLogisticsInfo(_self.validateForm.value).then(result => {
                                        if (result.status === 'success') {
                                            Object.assign(_self.tbOrder, CommonUtil.deleteNullAttrInObject(result.tbOrder));
                                            CommonUtil.toastNavigateBack('改为已发货操作成功！')
                                        } else {
                                            CommonUtil.toast(result.message);
                                        }
                                    });
                                }
                            }
                        });
                    } else {
                        console.log('验证失败');
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
